package com.near.events.activity;

import java.io.IOException;
import java.util.List;
import java.util.Locale;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.location.Address;
import android.location.Geocoder;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.provider.Settings;
import android.util.Log;
import android.widget.TextView;

public class EventsActivity extends Activity implements LocationListener {
	private static final long LOCATION_REFRESH_TIME = 0;
	private static final float LOCATION_REFRESH_DISTANCE = 0;
	private LocationManager locationManager;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.events);

		locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);

		if (isNetworkProviderEnabled()) {
			locationManager.requestLocationUpdates(
					LocationManager.NETWORK_PROVIDER, LOCATION_REFRESH_TIME,
					LOCATION_REFRESH_DISTANCE, this);
		} else if (isGpsProviderEnabled()) {
			locationManager.requestLocationUpdates(
					LocationManager.GPS_PROVIDER, LOCATION_REFRESH_TIME,
					LOCATION_REFRESH_DISTANCE, this);
		} else {
			showSettingsAlert();
		}
	}

	@Override
	public void onLocationChanged(Location location) {
		TextView locationTextView = (TextView) findViewById(R.id.locationTextView);
		String cityName = getCity(location.getLatitude(), location.getLongitude());
		locationTextView.setText(cityName);
	}
	@Override
	public void onProviderDisabled(String provider) {
		Log.d("Latitude", "disable");
	}

	@Override
	public void onProviderEnabled(String provider) {
		Log.d("Latitude", "enable");
	}

	@Override
	public void onStatusChanged(String provider, int status, Bundle extras) {
		Log.d("Latitude", "status");
	}
	
	private boolean isNetworkProviderEnabled() {
		return locationManager != null && locationManager
				.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
	}

	private boolean isGpsProviderEnabled() {
		return locationManager != null && locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
	}
	
	private void showSettingsAlert(){
		
        AlertDialog.Builder alertDialog = new AlertDialog.Builder(this);
        alertDialog.setTitle("GPS/Network settings");
  
        // Setting Dialog Message
        alertDialog.setMessage("GPS is not enabled and network is not available. Do you want to go to settings menu?");
  
        // Setting Icon to Dialog
        //alertDialog.setIcon(R.drawable.delete);
  
        // On pressing Settings button
        alertDialog.setPositiveButton("Settings", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog,int which) {
                Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
                startActivity(intent);
            }
        });
  
        // on pressing cancel button
        alertDialog.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
            dialog.cancel();
            }
        });
  
        // Showing Alert Message
        alertDialog.show();
    }
	
	private String getCity(double latitude, double longitude) {
		try {
			Geocoder gcd = new Geocoder(this, Locale.getDefault());
			List<Address> addresses = gcd.getFromLocation(latitude, longitude, 1);
			if (addresses.size() > 0)
				return addresses.get(0).getLocality();
		} catch (IOException e) {
		}

		return latitude + ", " + longitude;
	}
}
